//app.js

import wux from 'components/wux'

App({
  onLaunch: function() {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // this.getUserInfo();
    var that = this;
    // 将缓存中的电话号码赋值给全局的phone
    // wx.getStorage({
    //   key: 'phone',
    //   success: function(res) {
    //     console.log(res.data)
    //     that.globalData.phone = res.data;
    //   } 
    // }) 
    // 调取微信登录，获取openId
    wx.login({
      success: function(res) {
        console.log("登陆成功");
        console.log(res);
        // that.getOpenId(res.code);
      }, 
      fail: function() {
        console.log("登录失败");
      }
    });
    //获取手机系统，适配iPhoneX的自定义菜单栏
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.model,"model")
        console.log(res.pixelRatio)
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        console.log(res.language)
        console.log(res.version)
        console.log(res.platform)
        if (res.model.indexOf("iPhone X") != -1 || res.model.indexOf("iPhone XR") != -1 || res.model.indexOf("iPhone XS") != -1 || res.model.indexOf("iPhone XS Max") != -1){
          that.globalData.isBottomPhone = true;
        }  
        console.log(that.globalData.isBottomPhone,"that.globalData.isBottomPhone")
      }
    })
  },
  //获取openid
  getOpenId: function(code) {
    var that = this;
    console.log(that.globalData.BaseURL, "that.globalData.BaseURL")
    wx.request({
      url: that.globalData.BaseURL + '/api/wx/login' + "?code=" + code,
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },  
      // data: { 'code': code, 'xcxid': that.globalData.xcxidStr },
      success: function(res) { 
        var data = res.data.data;
        console.log(res.data.data, "res.data.data")
        that.globalData.openId = data.openid;
        that.globalData.sessionKey = data.sessionKey;
        console.log(that.globalData.openId, "that.globalData.openId")
        console.log(that.globalData.sessionKey, "that.globalData.sessionKey")
      },
      fail: function(res) {
        console.log("失败");
        that.globalData.openId = "";
      }
    })
  },
  //时间戳转换时间  
  toDate: function(number) {
    var n = number;
    var date = new Date(n);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    return (Y + M + D)
  },

  // getUserInfo: function(cb) {
  //   var that = this
  //   if (this.globalData.userInfo) {
  //     typeof cb == "function" && cb(this.globalData.userInfo)
  //   } else {
  //     //调用登录接口
  //     wx.getUserInfo({
  //       withCredentials: false,
  //       success: function(res) {
  //         var userInfo = res.userInfo;
  //         console.log('获取到的用户信息为:' + res.userInfo);
  //         that.globalData.nickName = userInfo.nickName;
  //         that.globalData.userInfo = userInfo
  //         typeof cb == "function" && cb(that.globalData.userInfo)
  //       }
  //     })
  //   }
  // },

  globalData: {
    BaseURL: "https://www.demo.com",
    accessToken: "",
    phone: "", 
    xcxidStr: "4",
    shopId: 0, 
    openId: null,
    sessionKey: null,       
    userId: null, //未使用
    viewId: null, //后台返回的用户viewId
    userInfo: null,//用户信息
    nickName: null,//用户昵称
    avatarUrl:null,//用户头像
    // 所处城市
    locationCity: {},
    // 选中城市
    selectedCity: {},
    // 选中蛋糕的spu信息
    cakeSpuItem: null,
    /* 
     *核对订单页面 
     */
    // 选中的地址信息
    addressList: null,
    // 选中的代金券信息
    couponItem: null,
    // 选中的配送时间和配送日期
    time: "",
    date: "",
    // 填写的备注信息
    remarkStr: "",
    // 填写的发票信息和纳税人信息
    fpInfo: "",
    nsrInfo: "",
    // 创建订单页自提收货信息
    zitiName: "",//自提人姓名
    zitiGender: "",//自提人性别
    zitiPhone:"",//自提人电话
    selectedShop: "",//自提门店
    isBottomPhone:"",//手机型号为iPhone，并且没有home键盘
  },
  // 通过scope来引入wux函数
  wux: (scope) => new wux(scope)
})